<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if(!function_exists('thumb'))
{
	function thumb($file, $w = 130, $h = 0, $crop = true, $newfile = null)
	{
		$CI = get_instance();

		$file = trim($CI->config->item('folder_image'), '/').'/'.$file;
		if(!class_exists('CI_image_lib'))
		{
			return $file;
		}

		if(!$file || !file_isFile(FCPATH. DIRECTORY_SEPARATOR .$file))
		{
			$file = $CI->config->item('noimage');
			if(!file_isFile(FCPATH. DIRECTORY_SEPARATOR .$file))
				return $file;
		}
		$image_library = 'gd2';
		$config = array();
		if(!$newfile)
		{
			$newfile = trim($CI->config->item('folder_cache'), '/').'/'.$file;
			$ex_name = $CI->image_lib->explode_name($newfile);
			$newfile = $ex_name['name'].'_'.$w.'x'.$h.($crop? 'c' : '').$ex_name['ext'];
			if(strtolower($ex_name['ext'])=='.gif')
			{
				$image_library = 'imagemagick';
				$config['library_path'] = '/usr/bin/convert';
			}
		}

		if(file_isFile(FCPATH. DIRECTORY_SEPARATOR .$newfile))
			return base_url().$newfile;

		folder_create(FCPATH. DIRECTORY_SEPARATOR .dirname($newfile));

		$config['image_library'] = $image_library;
		$config['source_image'] = FCPATH. DIRECTORY_SEPARATOR .$file;
		$config['new_image'] = FCPATH. DIRECTORY_SEPARATOR .$newfile;
		$config['width'] = $w;
		$config['height'] = $h;
		$config['crop'] = $crop;

		$CI->image_lib->initialize($config);
		if(!$CI->image_lib->make_thumb())
			return base_url().$file;

		return base_url().$newfile;
	}
}

if(!function_exists('produce_XML_object_tree'))
{
	function produce_XML_object_tree($raw_XML) {
		libxml_use_internal_errors(true);
		try {
			$xmlTree = new SimpleXMLElement($raw_XML);
		} catch (Exception $e) {
			// Something went wrong.
			$error_message = 'SimpleXMLElement threw an exception.';
			foreach(libxml_get_errors() as $error_line) {
				$error_message .= "\t" . $error_line->message;
			}
			trigger_error($error_message);
			return false;
		}
		return $xmlTree;
	}
}
if(!function_exists('file_get_contents_curl'))
{
	function file_get_contents_curl($url)
	{
		$curl = curl_init();

		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

		$data = curl_exec($curl);
		curl_close($curl);

		return $data;
	}
}